{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EO4tjJJ-OR0U"
   },
   "source": [
    "# Welcome to Phase-splitter\n",
    "Make sure to create a results folder and set the path to that in the funcs.py\n",
    "\n",
    "Welcome to the ultimate tool for assessment of insitu atomic pair distribution functions data with the help from machine learning.\n",
    "To use this algorithm you need:\n",
    "\n",
    "    1) A folder with in situ PDF data in .gr format\n",
    "\n",
    "Every plot and NMF components will be saved in results.    \n",
    "This algorithm normalize the data in respect to the global maximum and minimum in order to perform non-negative matrix factorization. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "executionInfo": {
     "elapsed": 1206,
     "status": "ok",
     "timestamp": 1647186814731,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "iKrCeRh9kn5B"
   },
   "outputs": [],
   "source": [
    "import funcs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 32546,
     "status": "ok",
     "timestamp": 1647186847269,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "-2Kq0435lDg7",
    "outputId": "db9515c0-10f2-42a8-ee13-45468598c22c"
   },
   "outputs": [],
   "source": [
    "data_dir = 'data'\n",
    "x, y, folder = funcs.get_in_situ_data(data_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 675
    },
    "executionInfo": {
     "elapsed": 11678,
     "status": "ok",
     "timestamp": 1647186858931,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "DR47QQIcTJEX",
    "outputId": "8208f56b-aa00-4828-dc53-ceca3e4d7fd2"
   },
   "outputs": [],
   "source": [
    "#Makes contour plot of all your data\n",
    "funcs.insitu_plot(x, y,folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "executionInfo": {
     "elapsed": 12574,
     "status": "ok",
     "timestamp": 1647186871500,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "gqn--zdzo8-9"
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "#Generate a Pearson matrix and does PCA on the data to estimate the amount of phase transitions\n",
    "funcs.gen_corr(y,folder)\n",
    "funcs.PCA_plot(funcs.Normalize(y),folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 663
    },
    "executionInfo": {
     "elapsed": 5501,
     "status": "ok",
     "timestamp": 1647186876965,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "5JP2ViIuo_Es",
    "outputId": "debefc62-c67a-4ad5-ef20-752081054c29"
   },
   "outputs": [],
   "source": [
    "funcs.plot(folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 331,
     "status": "ok",
     "timestamp": 1647186877280,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "22o0Arg3pALR",
    "outputId": "3f613856-6cf0-4103-cacf-901515a3ce18"
   },
   "outputs": [],
   "source": [
    "funcs.phase_estimation(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 590
    },
    "executionInfo": {
     "elapsed": 82537,
     "status": "ok",
     "timestamp": 1647186959805,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "v4WjDc6upBox",
    "outputId": "a4ab9b36-41bd-446d-f234-9d1e1fe0ddeb"
   },
   "outputs": [],
   "source": [
    "#Enter the desired number of components for NMF to compute\n",
    "funcs.NMF_cal(x,funcs.Normalize(y),folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "sb_61J1aQgys"
   },
   "source": [
    "Further data exploration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 688,
     "output_embedded_package_id": "1xnjExbY8VEHEy69zhAcfIwKWbleN7uSE"
    },
    "executionInfo": {
     "elapsed": 116521,
     "status": "ok",
     "timestamp": 1647187076322,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "awDXL4zUpEID",
    "outputId": "6392687a-fed8-40cf-8f74-820692b25217"
   },
   "outputs": [],
   "source": [
    "#This will make a movie of the all the PDFs (might be time consuming) \n",
    "#funcs.Dynamicgif(x,y,folder)\n",
    "#Unfortunally only works in colab at the moment "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "executionInfo": {
     "elapsed": 12332,
     "status": "ok",
     "timestamp": 1647187088618,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "khlsbZwspFUL"
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "#This shows approximatly where in the data the NMF components correspond to - together with a Pearson value of how close they match the direct PDF in the data\n",
    "funcs.pearson_nmf(x,y,folder)\n",
    "funcs.contour_nmf(x,y,folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 663
    },
    "executionInfo": {
     "elapsed": 6677,
     "status": "ok",
     "timestamp": 1647187095292,
     "user": {
      "displayName": "Joakim Lajer",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjiJMiAA7dZB0qJy0uyPJVaYVL3HPiamSKFsnT6_g=s64",
      "userId": "03758550362121344378"
     },
     "user_tz": -60
    },
    "id": "_hcINvMQpGOM",
    "outputId": "eb126775-fc94-4d46-b96c-7a2d61858ca7",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "funcs.plot_2(folder)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "Phase-splitter",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}